#pragma once

#include <iostream>
#include <string>
#include <ctime>
#include <pthread.h>

typedef void(*callback_t)();
static int num = 1;
class thread
{
public:
    static void* Routinue(void* args)
    {
        thread* tp=static_cast<thread*>(args);
        tp->Entery();
        return nullptr;
    }
public:
    thread(callback_t cb)
    :tid_(0)
    ,name_("no")
    ,start_timestamp_(0)
    ,isrunning_(false)
    ,cb_(cb)
    {

    }
    void Entery()
    {
        cb_();
    }
    void Join()
    {
        pthread_join(tid_,nullptr);
        isrunning_=false;
    }
     std::string Name()
    {
        return name_;
    }
    uint64_t StartTimestamp()
    {
        return start_timestamp_;
    }
    bool IsRunning()
    {
        return isrunning_;
    }
    void Run()
    {
        name_="thread-"+std::to_string(num++);
        start_timestamp_ = time(nullptr);
        isrunning_=true;
        pthread_create(&tid_,nullptr,Routinue,this);
    }
private:
    pthread_t tid_;
    std::string name_;
    bool isrunning_;
    uint64_t start_timestamp_;
    callback_t cb_;
};